package org.springblade.pm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Risk;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.feign.IXmglProjectClient;
import org.springblade.pm.service.IRiskService;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.RiskVo;
import org.springblade.pm.wrappper.RiskWrapper;
import org.springblade.system.entity.Dict;
import org.springblade.system.feign.IDictClient;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 风险
 */
@RestController
@RequestMapping("riskapi")
@AllArgsConstructor
public class RiskController {
	private IRiskService riskService;
	IDictClient dictClient;
	private IXmglProjectService xmglProjectService;

	@GetMapping("/getRiskDict")
	@ApiOperation(value = "获取新增风险计划的下拉框")
	public R getRiskDict(){
		HashMap<String, Object> map = new HashMap<>();
		//风险类别
		R<List<Dict>> riskRategory = dictClient.getList("risk_category");
		map.put("riskRategory",riskRategory.getData());
		//应对策略
		R<List<Dict>> preventiveSolution = dictClient.getList("preventive_solution");
		map.put("preventiveSolution",preventiveSolution.getData());
		//风险状态
		R<List<Dict>> riskStatus = dictClient.getList("risk_status");
		map.put("riskStatus",riskStatus.getData());
		//缓解措施执行情况
		R<List<Dict>> implementation = dictClient.getList("implementation");
		map.put("implementations",implementation.getData());
		//风险属性
		R<List<Dict>> riskAttributes = dictClient.getList("risk_attributes");
		map.put("riskAttributes",riskAttributes.getData());
		//可能发生阶段
		R<List<Dict>> possibleStage = dictClient.getList("possible_stage");
		map.put("possibleStage",possibleStage.getData());
		return R.data(map);
	}

	@GetMapping("/getDesByCategory")
	@ApiOperation(value = "根据风险类别获取风险描述列表")
	public R getDesByCategory(Integer category){
		List<Object> list = riskService.getDesByCategory(category);
		return R.data(list);
	}

	@GetMapping("/getInsertDefaults")
	@ApiOperation(value = "根据风险描述和风险类别查询出新增页面的默认值")
	public R getInsertDefaults(Integer category,String des){
			return R.data(riskService.getInsertDefaults(category,des));
	}

	@PostMapping("/saveRisk")
	@ApiOperation(value = "新增修改风险")
	public R saveRisk(Risk risk, BladeUser bladeUser){
		try {
			return R.data(riskService.saveRisk(risk,bladeUser.getUserId()));
		} catch (Exception e) {
			e.printStackTrace();
			return R.status(false);
		}
	}

	@GetMapping("/deleteRisk")
	@ApiOperation(value = "删除风险")
	public R deleteRisk(Integer id){
		return R.status(riskService.removeById(id));
	}

	@GetMapping("/riskList")
	@ApiOperation(value = "风险列表")
	public R riskList(Query query,String projectId){
		QueryWrapper<Risk> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",projectId);
		IPage<Risk> page = riskService.page(Condition.getPage(query),wrapper);
		List<RiskVo> riskVos = RiskWrapper.build().entityVOList(page.getRecords());
		IPage<RiskVo> riskVoIPage = RiskWrapper.build().pageVO(page);
		riskVoIPage.setRecords(riskVos);
		return R.data(riskVoIPage);
	}

	@GetMapping("/riskDetails")
	@ApiOperation(value = "风险详情")
	public R riskDetails(Integer id){
		Risk risk = riskService.getById(id);
		RiskVo riskVo = RiskWrapper.build().entityVO(risk);
		return R.data(riskVo);
	}


}
